//
//  SLInfoAffirmTip.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/20.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit
import MBProgressHUD
import SwiftyJSON

class SLInfoAffirmTip: UIViewController {
    var denotes:String = ""
    var json: JSON?
    var handle:SLBlock?
    override func viewDidLoad() {
        super.viewDidLoad()
//        if self.type == 1 {
        let array = self.json!["derivatives"].arrayValue
        self.titleL.text = "Please confirm"
        for i in 0...array.count-1{
            if let data = array[i].dictionary{
                if i == 0 {
                    self.nameView.titleL.text = data["uniqueness"]?.stringValue
                    self.nameView.inputTF.text = data["demonstrate"]?.stringValue
                }else if i == 1 {
                    self.idView.titleL.text = data["uniqueness"]?.stringValue
                    self.idView.inputTF.text = data["demonstrate"]?.stringValue
                }else if i == 2 {
                    self.dateView.titleL.text = data["uniqueness"]?.stringValue
                    self.dateView.inputTF.text = data["demonstrate"]?.stringValue
                }
            }
        }
        
        self.dateView.inputTF.isUserInteractionEnabled = false
        self.nextButton.setTitle("Next", for: .normal)

        setupUI()
    }
    @objc func datePick(){
        DatePicker.showDatePick(page: self) { value in
            self.dateView.inputTF.text = value as? String
        }
    }
    func setupUI(){
        self.closeBtn.isHidden = false
        self.view.backgroundColor = rgba(hex: "#000000", a: 0.7)
    }

    class func showAlert(page:UIViewController,json:JSON,denotes:String,handle: SLBlock?){
        let picker = SLInfoAffirmTip()
        picker.denotes = denotes
        picker.json = json
        picker.handle = handle
        picker.modalPresentationStyle = .overCurrentContext
        picker.modalTransitionStyle = .crossDissolve
        page.present(picker, animated: true)
    }
    lazy var backView:UIView = {
        let pt = create_view(bgColor: .white, supView: self.view) { make in
            make.width.equalTo(303)
            make.height.equalTo(418)
            make.centerY.equalToSuperview().offset(-29)
            make.centerX.equalToSuperview()
        }
        pt.layer.masksToBounds = true
        pt.layer.cornerRadius = 12
        return pt
    }()
    
    lazy var titleL:UILabel = {
        let pt = create_label(text: "Information confirmation", textColor: .black,textFont: SLFontConfig.bold(size: 18),alignment: .center, supView: self.backView) { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(32)
        }
        pt.numberOfLines = 0
        return pt
    }()
  
    lazy var nameView:InformationView = {
        let pt = InformationView()
        pt.titleL.text = "Name"
        self.backView.addSubview(pt)
        pt.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.height.equalTo(72)
            make.top.equalTo(78)
        }
        return pt
    }()
    lazy var idView:InformationView = {
        let pt = InformationView()
        pt.titleL.text = "ID no"
        self.backView.addSubview(pt)
        pt.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.height.equalTo(72)
            make.top.equalTo(self.nameView.snp.bottom).offset(14)
        }
        return pt
    }()
    lazy var dateView:InformationView = {
        let pt = InformationView()
        pt.titleL.text = "Birth date"
        let tap = UITapGestureRecognizer(target: self, action: #selector(datePick))
        pt.addGestureRecognizer(tap)
        self.backView.addSubview(pt)
        pt.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.height.equalTo(72)
            make.top.equalTo(self.idView.snp.bottom).offset(14)
        }
        return pt
    }()

   
    lazy var closeBtn:UIButton = {
        let pt = create_button(image: IMAGE("icon_clos"), supView: self.view, closure: { make in
            make.top.equalTo(self.backView.snp.bottom).offset(24)
            make.centerX.equalToSuperview()
            make.height.width.equalTo(34)
        }, selector: (target: self, action: #selector(buttonClick), event:.touchUpInside))
        pt.layer.cornerRadius = 12

        
        return pt
    }()
    lazy var nextButton:UIButton = {
        let pt = create_button(title: "Next", titleColor: .white,titleFont: SLFontConfig.medium16,bgColor: rgba(hex: "#1608EC"), image: nil, supView: self.backView, closure: { make in
            make.bottom.equalTo(-20)
            make.width.equalTo(177)
            make.height.equalTo(44)
            make.centerX.equalToSuperview()
        }, selector: (target: self, action: #selector(buttonClick), event:.touchUpInside))
        pt.layer.cornerRadius = 22
        return pt
    }()
    @objc func buttonClick(btn: UIButton){
        if btn == self.closeBtn{
            self.dismiss(animated: true)
        }else{
            let params = ["denotes":self.denotes,"suthat": self.nameView.inputTF.text ?? "",
                          "swing": self.idView.inputTF.text ?? "" ,
                          "chengdu" : self.dateView.inputTF.text ?? "",
                          "transfer" : "11"]
            SLHttpTool.AsyncReportData(path: "/satisfies/veins", params:params,target:self) { responseCall, success, msg, code in
                if success{
                    self.dismiss(animated: true)
                    if let block = self.handle{
                        block(0)
                    }
                }else{
                    MBProgressHUD.showError(error: msg)
                }
            
            }
        }
    }
}
class InformationView: UIView {
    lazy var titleL:UILabel = {
        let pt = create_label(text: "", textColor: rgba(hex: "#999999"),textFont: SLFontConfig.medium15, supView: self) { make in
            make.top.equalTo(0)
            make.left.equalTo(15)
            make.height.equalTo(20)
        }
        return pt
    }()
    lazy var tfView:UIView = {
        let v = create_view(bgColor: rgba(hex: "#F3F6F9"), supView: self) { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(28)
            make.height.equalTo(44)
        }
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 10
        return v
    }()
    lazy var inputTF:UITextField = {
        let pt = create_textField(text: "", textColor: .black,textFont: SLFontConfig.regular(size: 16), supView: self.tfView) { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }

        return pt
    }()
    lazy var line:UIView = {
        let pt = create_view(bgColor: rgba(hex: "#070707",a: 0.05), supView: self) { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(0)
            make.height.equalTo(1)
        }
//        pt.isHidden = true
        return pt
    }()

}
